/////////////////////////////////////////////////////////////////////////////////////
// WORKER THREAD DERIVED CLASS - produced by Worker Thread Class Generator Rel. 1.0
// GDIThread.cpp: Implementation of the CDataConverterThread Class.
/////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DataConverterThread.h"
#include "STSdataConverterDlg.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

UINT CDataConverterThread::nFinishThread = 0;
IMPLEMENT_DYNAMIC(CDataConverterThread, CThread)

/////////////////////////////////////////////////////////////////////////////////////
// Construction/Destruction
/////////////////////////////////////////////////////////////////////////////////////
CDataConverterThread::CDataConverterThread(CSTSdataConverterDlg* pWnd, const CString& inputFile,int year,int month,UINT CT )
{
	// support Thread-Handler-Oriented Synchronization
	SUPPORT_THREAD_SYNCHRONIZATION(CDataConverterThread)
	// support Notificable Thread
	SUPPORT_THREAD_NOTIFICATION

	m_pMainWnd = pWnd;
	m_inputFile = inputFile;
	m_year = year;
	m_month = month;
	m_CT = CT;
	TRACE(_T("%s,%d,%d,%d\r\n"),inputFile,year,month,CT);
}
CDataConverterThread::~CDataConverterThread()
{
}

/////////////////////////////////////////////////////////////////////////////////////
// CDataConverterThread diagnostics
#ifdef _DEBUG
void CDataConverterThread::AssertValid() const
{
	CThread::AssertValid();
}

void CDataConverterThread::Dump(CDumpContext& dc) const
{
	CThread::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////////////

void CDataConverterThread::OnInitThread()
{
}

void CDataConverterThread::OnExitThread()
{
	Lock();
	
	if(++nFinishThread == m_pMainWnd->vDataConverterThread.size())
		::PostMessage(m_pMainWnd->GetSafeHwnd(),WM_CONVERTING_COMPLETE,NULL,NULL);

	Unlock();
}

/////////////////////////////////////////////////////////////////////////////////////
// WORKER THREAD CLASS GENERATOR - Do not remove this method!
// MAIN THREAD HANDLER - The only method that must be implemented.
/////////////////////////////////////////////////////////////////////////////////////
DWORD CDataConverterThread::ThreadHandler()
{
	BOOL bContinue = TRUE;
	int nIncomingCommand;

	OnInitThread();

	do
	{
		WaitForNotification(nIncomingCommand, 0);

		/////////////////////////////////////////////////////////////////////////////
		//	Main Incoming Command Handling
		//
		//	This switch statement is just a basic skeleton showing the mechanism
		//	how to handle incoming commands. Developer may remove or rewrite
		//	necessary parts of this switch to fit his needs.
		/////////////////////////////////////////////////////////////////////////////
		switch (nIncomingCommand)
		{
		case CThread::CMD_TIMEOUT_ELAPSED:	// timeout-elapsing handling
			if (GetActivityStatus() != CThread::THREAD_PAUSED)
			{
				SingleStep();
			};
			break;

		case CThread::CMD_INITIALIZE:		// initialize the thread task
			// this command should be handled; it is automatically fired when the thread starts
			// UserSpecificOnInitializeHandler();
			HandleCommandImmediately(CMD_RUN);	// fire CThread::CMD_RUN command after proper initialization
			break;

		case CThread::CMD_RUN:				// handle 'OnRun' command
			if (GetActivityStatus() != CThread::THREAD_PAUSED)
			{
				SetActivityStatus(CThread::THREAD_RUNNING);

				MonthRecord(m_inputFile,m_year,m_month,m_CT);

				bContinue = FALSE;
			}
			break;

		case CThread::CMD_PAUSE:			// handle 'OnPause' command
			if (GetActivityStatus() != CThread::THREAD_PAUSED)
			{
				// UserSpecificOnPauseHandler();
				SetActivityStatus(CThread::THREAD_PAUSED);
			};
			break;

		case CThread::CMD_CONTINUE:			// handle 'OnContinue' command
			if (GetActivityStatus() == CThread::THREAD_PAUSED)
			{
				SetActivityStatus(CThread::THREAD_CONTINUING);
				// UserSpecificOnContinueHandler();
				HandleCommandImmediately(CMD_RUN);	// fire CThread::CMD_RUN command
			};
			break;

		case CThread::CMD_STOP:				// handle 'OnStop' command
			bContinue = FALSE;
			break;

		default:							// handle unknown commands...
			break;
		};

	} while (bContinue);

	OnExitThread();

	return (DWORD)CThread::DW_OK;			// if everything OK
}
